part07_age<- read.csv("part07_age.csv")
part09_age<- read.csv("part09_age.csv")
part11_age<- read.csv("part11_age.csv")
part13_age<- read.csv("part13_age.csv")
part15_age<- read.csv("part15_age.csv")
part17_age<- read.csv("part17_age.csv")
part19_age<- read.csv("part19_age.csv")
part21_age<- read.csv("part21_age.csv")

times100<- function(df){
  df$Mean<- df$Mean*100
  df$ci <- df$ci*100
  return(df)
}
part07_age<- times100(part07_age)
part09_age<- times100(part09_age)
part11_age<- times100(part11_age)
part13_age<- times100(part13_age)
part15_age<- times100(part15_age)
part17_age<- times100(part17_age)
part19_age<- times100(part19_age)
part21_age<- times100(part21_age)

#2013 vs 2017
df1317<- bind_rows(part13_age, part17_age)
gg1317_2<- ggplot(df1317, aes(x=age, y=Mean, colour=as.factor(yr))) + 
  geom_errorbar(aes(ymin=Mean-ci, ymax=Mean+ci), width=1) +
  geom_line() +
  geom_point()+ ggtitle("2013 to 2017")+xlab("Age")+ylab("Percent participation")
df1317<- bind_cols(part13_age, part17_age)
df1317$diff<- df1317$Mean...7- df1317$Mean...2
df1317$diffci<- 1.96*100*sqrt( ((df1317$Mean...7/100)*(1-df1317$Mean...7/100)/df1317$Freq...8) + 
                             ((df1317$Mean...2/100)*(1-df1317$Mean...2/100)/df1317$Freq...3))
gg1317_1<- ggplot(df1317, aes(x=age...1, y=diff)) + 
  geom_errorbar(aes(ymin=diff-diffci, ymax=diff+diffci), width=1) +
  geom_line() +
  geom_point()+geom_hline(yintercept=0, color="red")+ xlab("Age")+ylab("Increase in participation")+ggtitle(" ")

#2015 vs 2019
df1519<- bind_rows(part15_age, part19_age)
gg1519_2<- ggplot(df1519, aes(x=age, y=Mean, colour=as.factor(yr))) + 
  geom_errorbar(aes(ymin=Mean-ci, ymax=Mean+ci), width=1) +
  geom_line() +
  geom_point()+ ggtitle("2015 to 2019")+xlab("Age")+ylab("Percent participation")
df1519<- bind_cols(part15_age, part19_age)
df1519$diff<- df1519$Mean...7- df1519$Mean...2
df1519$diffci<- 1.96*100*sqrt( ((df1519$Mean...7/100)*(1-df1519$Mean...7/100)/df1519$Freq...8) + 
                             ((df1519$Mean...2/100)*(1-df1519$Mean...2/100)/df1519$Freq...3))
gg1519_1<- ggplot(df1519, aes(x=age...1, y=diff)) + 
  geom_errorbar(aes(ymin=diff-diffci, ymax=diff+diffci), width=1) +
  geom_line() +
  geom_point()+geom_hline(yintercept=0, color="red")+ xlab("Age")+ylab("Increase in participation")+ggtitle(" ")


#2013 vs 2021
df1321<- bind_rows(part13_age, part21_age)
gg1321_2<- ggplot(df1321, aes(x=age, y=Mean, colour=as.factor(yr))) + 
  geom_errorbar(aes(ymin=Mean-ci, ymax=Mean+ci), width=1) +
  geom_line() +
  geom_point()+ ggtitle("2013 to 2021")+xlab("Age")+ylab("Percent participation")
df1321<- bind_cols(part13_age, part21_age)
df1321$diff<- df1321$Mean...7- df1321$Mean...2
df1321$diffci<- 1.96*100*sqrt( ((df1321$Mean...7/100)*(1-df1321$Mean...7/100)/df1321$Freq...8) + 
                             ((df1321$Mean...2/100)*(1-df1321$Mean...2/100)/df1321$Freq...3))
gg1321_1<- ggplot(df1321, aes(x=age...1, y=diff)) + 
  geom_errorbar(aes(ymin=diff-diffci, ymax=diff+diffci), width=1) +
  geom_line() +
  geom_point()+geom_hline(yintercept=0, color="red")+ xlab("Age")+ylab("Increase in participation")+ggtitle(" ")


#pre to post comparisons (4/8 yr intervals to match on mayoral race) FIGURE A9
prow<-plot_grid(gg1317_2,gg1317_1,gg1519_2,gg1519_1,gg1321_2,gg1321_1, ncol=2)
prow


##### RACE BASED ESTIMATES
part07_wrurace<- read.csv("part07_wrurace.csv")
part09_wrurace<- read.csv("part09_wrurace.csv")
part11_wrurace<- read.csv("part11_wrurace.csv")
part13_wrurace<- read.csv("part13_wrurace.csv")
part15_wrurace<- read.csv("part15_wrurace.csv")
part17_wrurace<- read.csv("part17_wrurace.csv")
part19_wrurace<- read.csv("part19_wrurace.csv")
part21_wrurace<- read.csv("part21_wrurace.csv")
part07_wrurace<- times100(part07_wrurace)
part09_wrurace<- times100(part09_wrurace)
part11_wrurace<- times100(part11_wrurace)
part13_wrurace<- times100(part13_wrurace)
part15_wrurace<- times100(part15_wrurace)
part17_wrurace<- times100(part17_wrurace)
part19_wrurace<- times100(part19_wrurace)
part21_wrurace<- times100(part21_wrurace)



#2013 vs 2017
df1317<- bind_rows(part13_wrurace, part17_wrurace)
ggr1317_2<- ggplot(df1317, aes(x=wrurace, y=Mean, colour=as.factor(yr))) + 
  geom_errorbar(aes(ymin=Mean-ci, ymax=Mean+ci), width=1) +
  geom_line() +
  geom_point()+ ggtitle("2013 to 2017")+xlab("Ethnorace")+ylab("Percent participation")
df1317<- bind_cols(part13_wrurace, part17_wrurace)
df1317$diff<- df1317$Mean...7- df1317$Mean...2
df1317$diffci<- 1.96*100*sqrt( ((df1317$Mean...7/100)*(1-df1317$Mean...7/100)/df1317$Freq...8) + 
                             ((df1317$Mean...2/100)*(1-df1317$Mean...2/100)/df1317$Freq...3))
ggr1317_1<- ggplot(df1317, aes(x=wrurace...1, y=diff)) + 
  geom_errorbar(aes(ymin=diff-diffci, ymax=diff+diffci), width=1) +
  geom_line() +
  geom_point()+geom_hline(yintercept=0, color="red")+ xlab("Ethnorace")+ylab("Increase in participation")+ggtitle(" ")



#2015 vs 2019
df1519<- bind_rows(part15_wrurace, part19_wrurace)
ggr1519_2<- ggplot(df1519, aes(x=wrurace, y=Mean, colour=as.factor(yr))) + 
  geom_errorbar(aes(ymin=Mean-ci, ymax=Mean+ci), width=1) +
  geom_line() +
  geom_point()+ ggtitle("2015 to 2019")+xlab("Ethnorace")+ylab("Percent participation")
df1519<- bind_cols(part15_wrurace, part19_wrurace)
df1519$diff<- df1519$Mean...7- df1519$Mean...2
df1519$diffci<- 1.96*100*sqrt( ((df1519$Mean...7/100)*(1-df1519$Mean...7/100)/df1519$Freq...8) + 
                             ((df1519$Mean...2/100)*(1-df1519$Mean...2/100)/df1519$Freq...3))
ggr1519_1<- ggplot(df1519, aes(x=wrurace...1, y=diff)) + 
  geom_errorbar(aes(ymin=diff-diffci, ymax=diff+diffci), width=1) +
  geom_line() +
  geom_point()+geom_hline(yintercept=0, color="red")+ xlab("Ethnorace")+ylab("Increase in participation")+ggtitle(" ")
#2013 vs 2021?
df1321<- bind_rows(part13_wrurace, part21_wrurace)
ggr1321_2<- ggplot(df1321, aes(x=wrurace, y=Mean, colour=as.factor(yr))) + 
  geom_errorbar(aes(ymin=Mean-ci, ymax=Mean+ci), width=1) +
  geom_line() +
  geom_point()+ ggtitle("2013 to 2021")+xlab("Ethnorace")+ylab("Percent participation")
df1321<- bind_cols(part13_wrurace, part21_wrurace)
df1321$diff<- df1321$Mean...7- df1321$Mean...2
df1321$diffci<- 1.96*100*sqrt( ((df1321$Mean...7/100)*(1-df1321$Mean...7/100)/df1321$Freq...8) + 
                             ((df1321$Mean...2/100)*(1-df1321$Mean...2/100)/df1321$Freq...3))
ggr1321_1<- ggplot(df1321, aes(x=wrurace...1, y=diff)) + 
  geom_errorbar(aes(ymin=diff-diffci, ymax=diff+diffci), width=1) +
  geom_line() +
  geom_point()+geom_hline(yintercept=0, color="red")+ xlab("Ethnorace")+ylab("Increase in participation")+ggtitle(" ")



#pre to post comparisons (4/8 yr intervals to match on mayoral race) ###  FIG A10
prowr<-plot_grid(ggr1317_2,ggr1317_1,ggr1519_2,ggr1519_1,ggr1321_2,ggr1321_1, ncol=2)
prowr



####################
# learning? figs a7 and a8 
#################


#2017 vs 2021 (is diversity getting better? learning?)
df1721<- bind_rows(part17_wrurace, part21_wrurace)
ggr1721_2<- ggplot(df1721, aes(x=wrurace, y=Mean, colour=as.factor(yr))) + 
  geom_errorbar(aes(ymin=Mean-ci, ymax=Mean+ci), width=1) +
  geom_line() +
  geom_point()+ ggtitle("2017 to 2021")+xlab("Ethnorace")+ylab("Percent participation")
df1721<- bind_cols(part17_wrurace, part21_wrurace)
df1721$diff<- df1721$Mean...7- df1721$Mean...2
df1721$diffci<- 1.96*100*sqrt( ((df1721$Mean...7/100)*(1-df1721$Mean...7/100)/df1721$Freq...8) + 
                             ((df1721$Mean...2/100)*(1-df1721$Mean...2/100)/df1721$Freq...3))
ggr1721_1<-ggplot(df1721, aes(x=wrurace...1, y=diff)) + 
  geom_errorbar(aes(ymin=diff-diffci, ymax=diff+diffci), width=1) +
  geom_line() +
  geom_point()+geom_hline(yintercept=0, color="red")+ xlab("Ethnorace")+ylab("Increase in participation")+ggtitle(" ")


#2017 vs 2019
df1719<- bind_rows(part17_wrurace, part19_wrurace)
ggr1719_2<- ggplot(df1719, aes(x=wrurace, y=Mean, colour=as.factor(yr))) + 
  geom_errorbar(aes(ymin=Mean-ci, ymax=Mean+ci), width=1) +
  geom_line() +
  geom_point() + ggtitle("2017 to 2019")+xlab("Ethnorace")+ylab("Percent participation")
df1719<- bind_cols(part17_wrurace, part19_wrurace)
df1719$diff<- df1719$Mean...7- df1719$Mean...2
df1719$diffci<- 1.96*100*sqrt( ((df1719$Mean...7/100)*(1-df1719$Mean...7/100)/df1719$Freq...8) + 
                             ((df1719$Mean...2/100)*(1-df1719$Mean...2/100)/df1719$Freq...3))
ggr1719_1<-ggplot(df1719, aes(x=wrurace...1, y=diff)) + 
  geom_errorbar(aes(ymin=diff-diffci, ymax=diff+diffci), width=1) +
  geom_line() +
  geom_point()+geom_hline(yintercept=0, color="red")+ xlab("Ethnorace")+ylab("Increase in participation")+ggtitle(" ")

prow_lr<-plot_grid(ggr1719_2,ggr1719_1,ggr1721_2,ggr1721_1, ncol=2)
prow_lr #fig a8

#back 2 age

#2017 vs 2021 (is diversity getting better? learning?)
df1721<- bind_rows(part17_age, part21_age)
gg1721_2<- ggplot(df1721, aes(x=age, y=Mean, colour=as.factor(yr))) + 
  geom_errorbar(aes(ymin=Mean-ci, ymax=Mean+ci), width=1) +
  geom_line() +
  geom_point()+ ggtitle("2017 to 2021")+xlab("Age")+ylab("Percent participation")
df1721<- bind_cols(part17_age, part21_age)
df1721$diff<- df1721$Mean...7- df1721$Mean...2
df1721$diffci<- 1.96*100*sqrt( ((df1721$Mean...7/100)*(1-df1721$Mean...7/100)/df1721$Freq...8) + 
                             ((df1721$Mean...2/100)*(1-df1721$Mean...2/100)/df1721$Freq...3))
gg1721_1<-ggplot(df1721, aes(x=age...1, y=diff)) + 
  geom_errorbar(aes(ymin=diff-diffci, ymax=diff+diffci), width=1) +
  geom_line() +
  geom_point()+geom_hline(yintercept=0, color="red")+ xlab("Age")+ylab("Increase in participation")+ggtitle(" ")


#2017 vs 2019
df1719<- bind_rows(part17_age, part19_age)
gg1719_2<- ggplot(df1719, aes(x=age, y=Mean, colour=as.factor(yr))) + 
  geom_errorbar(aes(ymin=Mean-ci, ymax=Mean+ci), width=1) +
  geom_line() +
  geom_point() + ggtitle("2017 to 2019")+xlab("Age")+ylab("Percent participation")
df1719<- bind_cols(part17_age, part19_age)
df1719$diff<- df1719$Mean...7- df1719$Mean...2
df1719$diffci<- 1.96*100*sqrt( ((df1719$Mean...7/100)*(1-df1719$Mean...7/100)/df1719$Freq...8) + 
                             ((df1719$Mean...2/100)*(1-df1719$Mean...2/100)/df1719$Freq...3))
gg1719_1<-ggplot(df1719, aes(x=age...1, y=diff)) + 
  geom_errorbar(aes(ymin=diff-diffci, ymax=diff+diffci), width=1) +
  geom_line() +
  geom_point()+geom_hline(yintercept=0, color="red")+ xlab("Age")+ylab("Increase in participation")+ggtitle(" ")


#learning? 
prow_l<-plot_grid(gg1719_2,gg1719_1,gg1721_2,gg1721_1, ncol=2)
prow_l #fig a7
